var jName = getCookie('Jname');
var icloudNo = "";
//我的权限数组
var arrayTitle = new Array;
 findMyCatalogue();
$(document).ready(function() {
	initMySummernote("faileContentDetails");

	init(1, "", "", "");
});

//默认加载  
function init(pageNo, status, username, phone) {
	//获取用户信息列表
	$("#thislist").html("");
	$.ajax({
		url: urlcore + "/api/cashRecord/selectAll_List?currentPage=" + pageNo + "&status=" + status + "&username=" + username + "&phone=" + phone,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
				//i表示在data中的索引位置，n表示包含的信息的对象
				$.each(data.data.records, function(i, n) {
					var thislist = "";
					var id = n.id;
					var real_name = n.user.realName;
					var phone = n.user.phone;
					var payment_num = n.paymentNum;
					var gmt_datetime = n.gmtDatetime;
					var upt_datetime = n.uptDatetime;
					var pay_type;
					if(n.payType == 1) {
						pay_type = "白条支付";
					} else if(n.payType == 2) {
						pay_type = "支付宝支付";
					} else if(n.payType == 3) {
						pay_type = "微信支付";
					} else if(n.payType == 4) {
						pay_type = "银联支付";
					} else {
						pay_type = "未知";
					}
					var withdraw_status;
					if(n.withdrawStatus == 1) {
						withdraw_status = "申请中";
					} else if(n.withdrawStatus == 2) {
						withdraw_status = "已打款";
					} else if(n.withdrawStatus == 3) {
						withdraw_status = "拒绝";
					} else if(n.withdrawStatus == 4) {
						withdraw_status = "提现失败";
					}else if(n.withdrawStatus == 5) {
						withdraw_status = "提现中";
					}else {
						withdraw_status = "未知";
					}
					var statusTitle;
					if(n.status == 1) {
						statusTitle = "提现成功";
					} else if(n.status == 2) {
						statusTitle = "提现失败";
					}
					thislist += '<tr>' +
						'<td class="footable-visible">' + n.id + '</td>' +
						'<td class="footable-visible">' + real_name + '</td>' +
						'<td class="footable-visible">' + phone + '</td>' +
						'<td class="footable-visible">' + payment_num + '</td>' +
						'<td class="footable-visible">' + gmt_datetime + '</td>' +
						'<td class="footable-visible">' + pay_type + '</td>' +
						'<td class="footable-visible">' + upt_datetime + '</td>' +
						'<td class="footable-visible">' + withdraw_status + '</td>' +
						'<td class="footable-visible footable-last-column">' 
			if(withdraw_status=='拒绝'){
		thislist +=    	" <a class=\"\" hidden=\"hidden\" name=\"详细\" href=\"Withdrawals_details.html?id="+ id +"\"> 详细</a>\n"
					   }else if(withdraw_status=='申请中'){
		thislist +=		" <a class=\"\" hidden=\"hidden\" name=\"详细\" href=\"Withdrawals_details.html?id="+id+"\"> 详细</a>\n"+
						" <a class=\"\" hidden=\"hidden\" name=\"拒绝\" href=\"javascript:;\"  style=\"background-color: red;\"  onclick=\"deleteeach(" + id  + ")\"> 拒绝</a>\n"+
						" <a class=\"\" hidden=\"hidden\" name=\"同意\" href=\"javascript:;\"  onclick=\"payWithDraw(" + id  + ")\"> 同意</a>\n"
					    }else{
		thislist +=	  	" <a class=\"\" hidden=\"hidden\" name=\"详细\" href=\"Withdrawals_details.html?id="+ id +"\"> 详细</a>\n"
					    }
					$('#thislist').append(thislist);
				});
				$.each(arrayTitle, function(i, k) {
					$('a[name="' + k + '"]').attr("hidden", false).attr("class", "btn btn-primary btn-xs");
					$('a[data="topButton"]').attr("class", "btn btn-sm btn-primary");
				});
				$('#thiscount').text(data.data.total);
				$("#pager").pager({
					pagenumber: pageNo,
					pagecount: data.data.pages,
					totalcount: data.data.total,
					buttonClickCallback: PageClick
				});

			} else if(data.code == 'OVERTIME') {
				var thisUrl = window.location.href;
				if(thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href = "login.html";
				}

			} else {
				if(data.msg != '空数据') {
					alert(data.msg)
				} else {
					$('#thiscount').text(0);
				}
			}

		},
		error: function() {
			/* Act on the event */
			alert("error");
		}
	});
}

//回调函数  
PageClick = function(pageclickednumber) {
	var status = $('#status').val();
	var username = $("#username").val();
	var phone = $("#phone").val();
	init(status, username, phone, pageclickednumber);
}

function findMyCatalogue() {
	$.ajax({
		url: urlcore + "/api/roleThirdCatalogue/findAllByUser?secondTitle=" + jName,
		type: "GET",
		dataType: 'json',
		async: false,
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
				$.each(data.data, function(i, n) {
					arrayTitle.push(n.thirdCatalogue.title);
				});
			} else {
				alert(data.msg);
			}

		},
		error: function() {
			alert("error");
		}
	});
}

function searchList() {
	var status = $('#status').val();
	var username = $("#username").val();
	var phone = $("#phone").val();
	init(1, status, username, phone);
}

function newPassword(id) {
	if(confirm("确认同步？")) {
		$.ajax({
			url: urlcore + "/api/icloud/update/status?id=" + id + '&status=1',
			type: "post",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				loadMyEssay('');
			},
			error: function() {
				/* Act on the event */
				alert("error");
			}
		});
	}
}



function deleteeach(id) {
	if(confirm("确定拒绝？")) {
		$.ajax({
			url: urlcore + "/api/cashRecord/disableWithdraw?id=" + id + '&status=3',
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success:function(data){
			     window.location="Withdrawals_list.html"
			},
			error:function() {
				/* Act on the event */
				alert("error");
			}
		});	
	}
}

function payWithDraw(id) {
	if(confirm("确定打款？")) {
		$.ajax({
			url: urlcore + "/api/user/payWithDraw?id=" + id,
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success:function(data){
			     //window.location="Withdrawals_list.html"
			},
			error:function() {
				/* Act on the event */
				alert("error");
			}
		});	
	}
}

//查询失败原因
function details(id) {
	$.ajax({
		url: urlcore + "/api/cashRecord/selectOne?id=" + id,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			$('#faileContentDetails').summernote("code", data.data.reason);
		},
		error: function() {
			/* Act on the event */
			alert("error");
		}
	});
}


function findMyCatalogue(){
	$.ajax({
		url: urlcore + "/api/roleThirdCatalogue/findAllByUser?secondTitle="+jName,
		type: "GET",
		dataType: 'json',
		async: false,
		contentType: "application/json;charset=utf-8",
		success:function(data){
		if (data.success == true) {
			$.each(data.data, function(i,n) {
				arrayTitle.push(n.thirdCatalogue.title);
			});
		} else {
			alert(data.msg);
		}

		},
		error:function() {
			alert("error");
		}
	});
}
